<template>
  <div class="container-fluid">
    <div class="row school-model school-tiao">
      <div class="col school-tiao-left">
        <span>{{ $route.name }}</span>
      </div>
      <div class="col text-right">
        <a-button @click="add()" type="primary">添加上课时段</a-button>
      </div>
    </div>
    <div class="row school-model index-model">
      <div class="col school-col-table">
        <el-table :data="getschoolclasstime" style="width: 100%; border: 1px solid #eeeeee">
          <el-table-column prop="ID" label="序号" width="60">
            <template slot-scope="scope">{{scope.$index+1}}</template>>
          </el-table-column>
          <el-table-column prop="endtime" label="上课时间">
            <template slot-scope="scope">{{ gettime(scope.row) }}</template>
          </el-table-column>
          <el-table-column prop="schooladdressid" label="校区">
            <template
              slot-scope="scope"
            >{{ scope.row.schooladdressid!==0?getschooladdress.find(v=> v.ID == scope.row.schooladdressid).name:'全部校区' }}</template>
          </el-table-column>
          <el-table-column align="right" prop="detailedaddress" label="操作">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="add(scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="del(scope.row.ID)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-dialog class="school-form" :title="title" width="510px" :visible.sync="setaddress">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" size="mini">
            <el-form-item label="上课时间：" prop="starttime">
              <el-time-select
                placeholder="上课时间："
                v-model="ruleForm.starttime"
                :picker-options="{start: '07:00',step: '00:05',end: '21:30'}"
              ></el-time-select>
            </el-form-item>
            <el-form-item label="下课时间：" prop="endtime">
              <el-time-select
                placeholder="下课时间："
                v-model="ruleForm.endtime"
                :picker-options="{start: '07:30',step: '00:05',end: '22:00',minTime: ruleForm.starttime}"
              ></el-time-select>
            </el-form-item>
            <el-form-item label="校区:" prop="schooladdressid">
              <el-select v-model="ruleForm.schooladdressid" allow-create placeholder="请选择校区">
                <el-option label="全部校区" :value="0"></el-option>
                <el-option
                  v-for="(item, i) in getschooladdress"
                  :key="`1class${i}`"
                  :label="item.name"
                  :value="item.ID"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" size="mini" @click="submitForm">确 定</el-button>
            <el-button @click="setaddress = false" size="mini">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  asynccreatschoolclasstime,
  asyncdelschoolclasstime
} from "../../../../plugins/api.js";
export default {
  name: "settime",
  data() {
    return {
      setaddress: false,
      title: "添加时间",
      ruleForm: {
        ID: "",
        starttime: "",
        endtime: "",
        schooladdressid: ""
      },
      rules: {
        schooladdressid: [
          { required: true, message: "请选择校区", trigger: "change" }
        ],
        starttime: [
          { required: true, message: "请选择开始时间", trigger: "change" }
        ],
        endtime: [
          { required: true, message: "请选择结束时间", trigger: "change" }
        ]
      }
    };
  },
  computed: mapGetters(["getschoolclasstime", "getschooladdress"]),
  methods: {
    gettime(row) {
      const { starttime, endtime } = row;

      return `${starttime}-${endtime}`;
    },
    add(v) {
      this.setaddress = true;
      if (v) {
        this.title = "修改时间";
        this.ruleForm = JSON.parse(JSON.stringify(v));
      } else {
        this.title = "添加时间";
        this.ruleForm = {
          starttime: "",
          endtime: "",
          schooladdressid: 0
        };
      }
    },
    del(ID) {
      this.$confirm("此操作将永久删除该上课时段, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          try {
            await asyncdelschoolclasstime({ ID });
            await this.$store.dispatch("actionschoolclasstime");
            this.$message.success("删除成功");
          } catch (err) {
            this.$message.error("删除失败");
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async submitForm() {
      this.$refs["ruleForm"].validate(async valid => {
        if (valid) {
          if (this.$store.state.state) return;
          this.$store.commit("setstate", true);
          const data = this.ruleForm.ID ? "修改" : " 添加";
          try {
            await asynccreatschoolclasstime(this.ruleForm);
            await this.$store.dispatch("actionschoolclasstime");
            this.setaddress = false;
            this.$message.success(`${data}成功`);
          } catch (err) {
            this.$message.error(`${data}失败`);
          }
          this.$store.commit("setstate", false);
        } else {
          console.log("error submit!!");

          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.index-model {
  margin-top: 13px;
}
</style>